package cn.ly.lock;

//定义一把锁
public abstract class AbstractLock implements Lock {

    //模板方法
    @Override
    public void getLock() {
        if(tryLock()){
            System.out.println(">>>>>>>获得锁"+Thread.currentThread().getName());
        }else{
//            System.out.println(">>>>>>>未获得锁，线程将等待"+Thread.currentThread().getName());
            waitLock();
            getLock();  //递归调用，尝试获取锁
        }
    }

    public abstract boolean tryLock();

    public abstract void waitLock();
}
